package com.wanmait.happyheating.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.wanmait.happyheating.pojo.MaterialsName;
import com.wanmait.happyheating.pojo.MaterialsType;
import com.wanmait.happyheating.service.MaterialsNameService;
import com.wanmait.happyheating.util.Result;
import org.springframework.http.converter.json.GsonBuilderUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 耗材相关的controller，添加耗材
 * @author 明康 2021-5-17
 */
@RestController
@RequestMapping("/manage")
public class AdminMaterialsNameController {

    @Resource
    private MaterialsNameService materialsNameService;

    /*
      查重
     */
    @RequestMapping("findMaterialsName")
    public Result findMaterialsName(@RequestBody MaterialsName materialsName){
        LambdaQueryWrapper<MaterialsName> materialsNameLambdaQueryWrapper = new LambdaQueryWrapper<>();
        materialsNameLambdaQueryWrapper.eq(MaterialsName::getTitle,materialsName.getTitle());
        List<MaterialsName> materialsNames = materialsNameService.list(materialsNameLambdaQueryWrapper);
        if(materialsNames.size()>0){
           return Result.error("有重复！");
        }
        {
            return Result.success("无重复！");
        }
    }

    /*
    添加耗材
     */

    @RequestMapping("addMaterials")
    public Result addMaterials(@RequestBody MaterialsName materialsName ){

        List<MaterialsName> allMaterials = materialsNameService.list();

        for(MaterialsName materialsName1:allMaterials){
            if(materialsName1.getTitle().equals(materialsName.getTitle())){
                return Result.error("该材料已存在，请勿重复添加");
            }
        }
        materialsNameService.save(materialsName);
         return Result.success("添加成功！");
    }
    /*
    修改耗材信息
     */
    @RequestMapping("updateMaterialsName")
    public Result updateMaterialsName(@RequestBody MaterialsName materialsName){
        if(materialsName.getId()!=0){
            Boolean flag = materialsNameService.updateById(materialsName);
            if(flag){
                return Result.success("修改成功！");
            }else{
                return Result.error("修改失败");
            }
        }
        return Result.error("修改失败，无效的Id");
    }

    /*
    删除耗材
     */
    @DeleteMapping("removeMaterialsName{id:\\d+}")
    public Result removeMaterialsName(@PathVariable Integer id){
        if(id!=0){
            boolean flag = materialsNameService.removeById(id);
            if(flag){
                return Result.success("删除成功！");
            }else{
                return Result.error("删除失败！");
            }
        }
        return Result.error("删除失败，无效的Id");
    }

    /*
    批量删除
     */
    @RequestMapping("removeMaterialsNames")
    public Result removeMaterialsNames(@RequestBody List<Integer> materialsNameIds){
        if(materialsNameIds.size()>0){
            Boolean flag = materialsNameService.removeByIds(materialsNameIds);
            if(flag){
                return Result.success("删除成功！");
            }
            else{
                return Result.error("删除失败！");
            }

        }
        return Result.error("删除失败，无效的数据！");
    }

    /*
    查询所有耗材
     */
    @RequestMapping("showMaterialsName")
    public Result showMaterials(@RequestBody MaterialsName materialsName,Integer pageNum,Integer pageSize){
        System.out.println(materialsName.getTitle()+"-------");
        if(pageNum==null){
            pageNum=1;
        }
        if(materialsName.getId()==0&&!("".equals(materialsName.getTitle()))){
            PageInfo<MaterialsName> materialsNames = materialsNameService.likeMaterialsNameByPage(pageNum,pageSize,materialsName.getTitle());
            return Result.success("查询成功！",materialsNames);
        }else{
            PageInfo<MaterialsName> materialsNames = materialsNameService.findAllMaterialsNames(pageNum,pageSize);
            return Result.success("查询成功！",materialsNames);
        }
    }

    /*
    根据分类查询耗材信息
     */
    @RequestMapping("findMaterialsNameByType")
    public Result findMaterialsNameByType(@RequestBody MaterialsType materialsType){
        if(materialsType!=null){
            LambdaQueryWrapper<MaterialsName> materialsNameLambdaQueryWrapper = new LambdaQueryWrapper<>();
            materialsNameLambdaQueryWrapper.eq(MaterialsName::getMaterialsTypeId,materialsType.getId());
            List<MaterialsName> materialsNames = materialsNameService.list(materialsNameLambdaQueryWrapper);
            materialsNames.forEach(materialsName -> System.out.println(materialsName.getTitle()));
            return Result.success("查询成功！",materialsNames);
        }
        return Result.error("查询失败，无效的Id");
    }
}
